package dper.study.codes.elasticsearch.service;

import dper.study.codes.common.util.JsonUtil;
import dper.study.codes.elasticsearch.api.GitInfoESAPI;
import dper.study.codes.elasticsearch.model.AuthorModel;
import dper.study.codes.elasticsearch.model.GitRepoModel;
import dper.study.codes.elasticsearch.utils.ElasticsearchUtil;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leichen on 2017/3/13.
 */
public class GitInfoESService implements GitInfoESAPI {

    @Autowired
    private ElasticsearchUtil elasticsearchUtil ;

    private final static String GITINFO_INDEXNAME = "index_gitinfo" ;

    private final static String GITINFO_GITREPO_TYPENAME = "type_gi_gitrepo" ;

    private final static String GITINFO_AUTHOR_TYPENAME = "type_gi_author" ;

    private final static String GITINFO_REPONAME_AUTOCOMPELETION = "reponame_ac_suggest" ;

    public BulkResponse indexGitRepoModel(List<GitRepoModel> gitRepoModelList) {
        List<String> jsonDataList = new ArrayList<String>() ;
        for(GitRepoModel gitRepoModel:gitRepoModelList){
            jsonDataList.add(JsonUtil.bean2json(gitRepoModel));
        }
        return elasticsearchUtil.createBulkIndex(GITINFO_INDEXNAME,GITINFO_GITREPO_TYPENAME,jsonDataList) ;
    }

    public BulkResponse indexAuthorModel(List<AuthorModel> authorModelList) {
        List<String> jsonDataList = new ArrayList<String>() ;
        for(AuthorModel authorModel:authorModelList){
            jsonDataList.add(JsonUtil.bean2json(authorModel));
        }
        return elasticsearchUtil.createBulkIndex(GITINFO_INDEXNAME,GITINFO_AUTHOR_TYPENAME,jsonDataList) ;
    }

    public SuggestResponse getGitRepoNameAutoCompletion(String gitRepoNamePrefix) {
        CompletionSuggestionBuilder acSuggestionsBuilder = new CompletionSuggestionBuilder(
                GITINFO_REPONAME_AUTOCOMPELETION);
        acSuggestionsBuilder.analyzer("ik")
                .text(gitRepoNamePrefix)
                .field(GITINFO_REPONAME_AUTOCOMPELETION).size(10) ;
        return elasticsearchUtil.suggestSearcher(GITINFO_INDEXNAME,acSuggestionsBuilder);
    }

    public static void main(String[] argv){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_es_beans.xml") ;
        GitInfoESService gitInfoESService = (GitInfoESService)ac.getBean("gitInfoESService") ;
        GitRepoModel gitRepoModel = new  GitRepoModel() ;
        gitRepoModel.setAuthorId(1);
        gitRepoModel.setDescription("dper-study-codes test");
        gitRepoModel.setId(1);
        gitRepoModel.setRepoName("dper-study-codes");
        gitRepoModel.setUrl("https://git.oschina.net/skyscraper/dper-study-codes.git");
        List<GitRepoModel> gitRepoModelList = new ArrayList<GitRepoModel>() ;
        gitRepoModelList.add(gitRepoModel) ;
        System.out.println(gitInfoESService.indexGitRepoModel(gitRepoModelList)) ;
    }
}
